import { create, update, remove, query } from '../../../../../framework/service/index';
import { ToastTips } from '../../../../../common/toastTips/tosat';

export default {

  namespace: 'myPartTime',

  state: {
    date: [],
    selectedIndex:0,
    pageNum:1,
    pageSize:10,
    pages:0,
    callCustomerStatus:false,
    customerPhone:''
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/my-part-time') {
          dispatch({
            type: 'query',
          })
        }
      })
    },
  },



  effects: {
    *query({ payload }, { call, put, select }) {

      const { selectedIndex, pageSize } = yield select (({myPartTime}) => myPartTime);
      const pageNum = payload && payload.pageNum ? payload.pageNum : 1;

      let status = '';
      if(selectedIndex == 1){
        status = 'AUDITING';
      }else if(selectedIndex == 2){
        status = 'CONFIRMING';
      }else if(selectedIndex == 3){
        status = 'WORKING';
      }else if(selectedIndex == 4){
        status = 'SETTLED';
      }else if(selectedIndex == 0){
        status = 'ALL';
      }
      console.log('status',status);

      const { code, data, message } = yield call(query, '/api/applicant/applications', {
        pageNum,
        pageSize,
        status
      });

      if ( code == 200 && data ) {
        // console.log('我的兼职', JSON.stringify(data));

        const currentList = yield select(({ partTime }) => partTime.jobList);
        let list = data.records;
        if (pageNum > 1) {
          list = currentList.concat(data.records);
        }
        let newList = [];
        list.map((item, index) => {
          // if(item.status == 'APPLYING'){
          //   newList.push(item);
          // }
          newList.push(item);
        })
        yield put({
          type: 'querySuccess',
          payload: {
            list: newList,
            pageNum: data.current,
            pageSize: data.size,
            pages: data.pages,
          }
        })
      }

    },

    //签到
    *signIn({ payload }, { call, put }) {

      const { applicationId, signCode } = payload;
      // console.log('applicantId',applicantId);
      // console.log('signCode',signCode);
      const subData = {
        applicationId,
        signCode
      }

      console.log('subData', JSON.stringify(subData));

      const { code, data, message } = yield call(create, `/api/application/sign`, {
        ...subData
      });
      console.log('code',code);
      console.log('data',data);
      console.log('message',message);
      if(code == 200){
        ToastTips('签到成功', 2000, null);
        yield put({
          type: 'query',
          payload: {
          }
        })
      }else{
        ToastTips('签到失败', 2000, null);
      }
    },

    //提醒结算
    *remindingAndClearing({ payload }, { call, put }) {

      const { applicationId } = payload;
      // console.log('subData', JSON.stringify(subData));
      const { code, data, message } = yield call(update, `/api/application/${applicationId}/remind`, {
      });
      // console.log('code',code);
      // console.log('data',data);
      // console.log('message',message);
      if(code == 200){
        ToastTips('提交成功', 2000, null);
        yield put({
          type: 'query',
          payload: {
          }
        })
      }else{
        ToastTips('提交失败', 2000, null);
      }
    }
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
